package com.edu.admin.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edu.admin.common.BaseService;
import com.edu.admin.entity.SysPhase;
import com.edu.admin.enums.ExceptionEnum;
import com.edu.admin.exception.SysException;
import com.edu.admin.mapper.PhaseMapper;
import com.edu.admin.vo.PhaseVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 学段服务
 * 
 * @author Wangjiong
 * 
 */
@Transactional
@Service
public class PhaseService extends BaseService<PhaseMapper, SysPhase> {
	/**
	 * 获取学段列表
	 * 
	 * @param 
	 * @return List<PhaseVo>
	 */
	public List<PhaseVo> getPhaseList() {
		return this.mapper.getPhaseList();
	}
	
	/**
	 * 获取学段列表（分页）
	 * 
	 * @param pageNum
	 * @param pageSize
	 * 
	 * @return PageInfo<PhaseVo>
	 */
	public PageInfo<PhaseVo> getPhaseListPage(Integer pageNum, Integer pageSize) {
		if (pageNum == null) {
			pageNum = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		PageHelper.startPage(pageNum, pageSize);
		
		List<PhaseVo> phaseList = this.mapper.getPhaseList();
		
		PageInfo<PhaseVo> pageInfo = new PageInfo<PhaseVo>(phaseList);
		
		return pageInfo;
	}
	
	/**
     * 学段重复性检查
     * @param SysPhase phase
     * 
     * @return List<PhaseVo>
     * @throws Exception
     */
    public List<PhaseVo> checkPhase(SysPhase phase) throws Exception {
    	
    	return this.mapper.findPhaseByPhaseName(phase.getPhaseId(), phase.getPhaseName().trim());
  
    }
	
	
	/**
	 * 学段保存
	 * 
	 * @param SysPhase phase
	 * 
	 * @return Map<String, Object>
	 * @throws Exception 
	 */
	public Map<String, Object> savePhase(SysPhase phase) throws Exception {
		Map<String, Object> result = new HashMap<>();
		
		List<PhaseVo> phaseList = checkPhase(phase);
		
		if (phaseList!=null && phaseList.size()>0)
		{
			throw new SysException(ExceptionEnum.PHASENAMEREPEAT);
		}
		
		Long phaseId = phase.getPhaseId();

		if (phaseId == null) {
				
			insert(phase);
			phaseId = phase.getPhaseId();
	
		} else {
	
			updateByPrimaryKeySelective(phase);

		}

		result.put("phaseId", phaseId);

		return result;
	}
	
	/**
	 * 删除学段
	 * 
	 * @param phaseId
	 * 
	 * @return int
	 */
	public int deletePhase(Long phaseId) {
		
		if (phaseId != null) {
			return this.deleteByPrimaryKey(phaseId);

		}
		
		return 0;	

	}


}
